Function.prototype.method = function (name, fn) {
    this.prototype[name] = fn;
    return this;
};
function init($) {
    var ajax = $.ajax;
    $.ajax = function (options) {
        var success = options.success;
        if (success) {
            options.success = function (content) {
                if (typeof content == "string") {
                    content = eval("(" + content + ")");
                }
                content.success == true ? success(content) : content.msg && alert(content.msg);
            };
        }
        return ajax(options);
    };
}
//init(jQuery);
var cjw_common = {
    cloneArray: function (list, callback) {
        var len = list.length,
            newList = new Array(len);
        for (var i = 0; i < len; i++) {
            newList[i] = callback ? callback(list[i]) : list[i];
        }
        return newList;
    },
    //模板
    transformTemplate: function (_template, data) {
        if (typeof _template != "string") {
            _template = $("<div></div>").html(_template).html();
        }
        var reg = /(\$\w+)/g,
            matchList = _template.match(reg);
        if (matchList) {
            var matchList2 = this.cloneArray(matchList, function (item) {
                return item.substring(1, item.length);
            });
            var list = [], len1 = matchList.length, len2 = data.length;
            for (var i = 0; i < len2; i++) {
                var _template2 = _template;
                for (var j = 0; j < len1; j++) {
                    _template2 = _template2.replace(matchList[j], data[i][matchList2[j]] || "");
                }
                list[i] = _template2;
            }
            return list.join('');
        }
    },
    //弹出框
    popBox: function (htm) {
        if ($(".popBox").length) {
            console.log("已存在.popBox");
            return;
        }
        var div = '<div class="popBox"></div><div id="zhezhao"></div>',
            fa = '<a id="close"><i class="fa fa-times"></i></a>',
            $div = $(div),
            $box = $div.eq(0);
        //使用.box-title标记标题框
        $box.append(htm).find(".box-title").append(fa);
        $box.close = function (callback) {
            if (callback) {
                try {
                    callback();
                }
                catch (e) {
                    console.log(e);
                }
            }
            $div.remove();
        }
        $box.find("a#close").click(function () {
            $box.close();
        })
        $(document.body).append($div);
        var disparity;
        if (disparity = $box.width() - 498) {
            $box.css("left", "calc(50% - " + $box.width() / 2 + "px)");
        }
        if (disparity = $box.height() - 298) {
            $box.css("top", "calc(50% - " + $box.height() / 2 + "px)");
        }
        return $box;
        //($box.css("top").replace("px", "") - disparity / 2)
    }
};